package freemarkertest;


import com.xuecheng.freemarker.domain.Student;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * - @author by 姜暄煊
 * - @Date 12.17
 * - @Description
 */
public class test {

    @Test
    public void test10() throws IOException, TemplateException {
        //创建配置信息
        Configuration configuration =
                new Configuration(Configuration.VERSION_2_3_30);


            //对配置类进行配置
        String path = this.getClass().getResource("/templates").getPath();
        configuration.setDirectoryForTemplateLoading(new File(path));
        configuration.setDefaultEncoding("utf-8");

        //获得模板文件对象
        Template template = configuration.getTemplate("02-basic.ftl");

        //获得数据模型

        Map date = getData();

        //静态化操作
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, date);
        InputStream inputStream = IOUtils.toInputStream(content);
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/02-basic.html"));

        IOUtils.copy(inputStream,fileOutputStream);


    }

    private Map getData() {
        Map<String, Object> map = new HashMap<>();
        Student student = new Student();
        student.setName("小明");
        student.setAge(18);

        //1.纯文本形式的参数
        map.put("name", "freemarker");
        //2.实体类相关的参数
        map.put("stu1", student);

        //2.1 小强对象模型数据
        Student stu1 = new Student();
        // stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());

        //2.2 小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);

        //2.3 将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);

        //2.4 向map中存放List集合数据
        map.put("stus", stus);


        //3. 创建Map数据
        HashMap<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);
        // 3.1 向map中存放Map数据
        map.put("stuMap", stuMap);

        HashMap<String, Student> stuMapTest = new HashMap<>();
        stuMapTest.put("stu.name", stu1);
        map.put("stuMapTest", stuMapTest);

        //返回Map
        return map;
    }
    @Test
    public void testHtml02() throws IOException, TemplateException {
        // 1.创建配置类信息
        Configuration configuration = new Configuration(Configuration.getVersion());


        // 2.对配置类进行配置
        //2.1 获得字符串模板内容
        String template = "" +
                "<html>\n" +
                "    <head></head>\n" +
                "    <body>\n" +
                "    名称：${name}\n" +
                "    </body>\n" +
                "</html>";
        //2.2 创建出字符串模板加载器
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        stringTemplateLoader.putTemplate("template", template);
        configuration.setTemplateLoader(stringTemplateLoader);

        // 3.获得模板文件对象
        Template configurationTemplate = configuration.getTemplate("template","utf-8");
        // 4.获得数据模型
        Map data = getData();

        // 5.静态化操作
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(configurationTemplate, data);
        InputStream inputStream = IOUtils.toInputStream(content);
        FileOutputStream outputStream = new FileOutputStream(new File("d:/index.html"));
        IOUtils.copy(inputStream, outputStream);
    }
}
